package com.yx.shop.service.impl;

import com.yx.shop.dao.CartDao;
import com.yx.shop.dao.ProductDao;
import com.yx.shop.dao.impl.CartDaoImpl;
import com.yx.shop.dao.impl.ProductDaoImpl;
import com.yx.shop.entitly.Cart;
import com.yx.shop.entitly.Product;
import com.yx.shop.service.CartService;
import com.yx.shop.utils.JDBCUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Classname CartServiceImpl
 * @Description TODO:
 * @Date 2021/05/07 23:11
 * @Created by yx
 */
public class CartServiceImpl implements CartService {
    private CartDao cartDao = new CartDaoImpl();
    private ProductDao productDao = new ProductDaoImpl();
    @Override
    public void createCart(Integer uid, Integer pid) {
        //1.判断购物车该商品是否存在
        Cart cart = cartDao.hasCart(uid,pid);
        if (cart!=null){
            //2.存在,修改数量与大小
            cart.setCnum(cart.getCnum()+1);
            cartDao.updateCart(cart);
        }else {
            //3.不存在添加
            Product product = productDao.getById(pid);

            //没有,创建一个新的购物车
            cart = new Cart();
            //商品数量初始为1
            cart.setCnum(1);
            cart.setPid(pid);
            cart.setProduct(product);
            cart.setUid(uid);

            //添加
            cartDao.insertCart(cart);
        }
    }

    @Override
    public List<Cart> getCartByUid(Integer uid) {
        List<Cart> cartList = null;
        try {
            JDBCUtils.begin();
            cartList = cartDao.getAllCartByUid(uid);
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }

        return cartList;
    }

    @Override
    public boolean clearCart(Integer pid, int uid) {
        boolean flag = false;
        try {
            JDBCUtils.begin();
            //判断是清空购物车还是删除单个商品
            if (pid==0){
                //清空购物车
                if (cartDao.clearCart(uid)>0){
                    //购物车清空成功
                    flag=true;
                }
            }else {
                //删除单个商品
                if (cartDao.deleteCart(pid,uid)>0){
                    //删除单个商品成功
                    flag=true;
                }
            }
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * @Author:yx
     * @return java.lang.Boolean
     * @Date 2021/05/08 19:43
     * @param uid
     * @param pid
     * @param num
     * @param pprice
     * @Description:修改购物车商品数量
     **/
    @Override
    public Boolean updateCart(int uid, Integer pid, Integer num, Double pprice) {
        boolean flag = false;
        try {
            JDBCUtils.begin();
            //判断num的值:num 1 0 -1
            //数量+1 删除 数量-1
            if (num==0){
                if (cartDao.deleteCart(pid,uid)>0) {
                    //删除成功
                    flag =  true;
                }
            }else {
                Cart cart = cartDao.hasCart(uid, pid);
                if (cart!=null){
                    //设置商品数量及小计
                    cart.setCnum(cart.getCnum()+num);
                    cart.setCcount(BigDecimal.valueOf(cart.getCnum()*pprice));
                    if (cartDao.updateCart(cart)>0){
                        //修改成功
                        flag =  true;
                    }
                }
            }
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return flag;
    }


}
